<script>
export default {
  name: 'RoleTools',
  cacheKey: 'TINY_ROLE_',
  checkAction:function (currAction) {
    var actionList=JSON.parse(localStorage.getItem(this.cacheKey+'_ACTION_LIST'))
    // 超级管理员
    if (actionList === '["*"]') {
      return true
    }
    return actionList.indexOf(currAction)>-1
  },
  getActionList:function () {
    var actionList=JSON.parse(localStorage.getItem(this.cacheKey+'_ACTION_LIST'))
    return actionList?actionList:[]
  },
  // 存储访问节点
  cacheAction: function (actionList) {
    localStorage.setItem(this.cacheKey+'_ACTION_LIST', JSON.stringify(actionList))
  },
  // 对比权限,生成菜单列表
  generateNav: function (nav_arr) {
    var actionList=JSON.parse(localStorage.getItem(this.cacheKey+'_ACTION_LIST'))
    // 超级管理员
    if (actionList === '["*"]') {
      return nav_arr
    }
    // 其他管理员
    var allArr = []
    actionList=JSON.parse(actionList)
    nav_arr.forEach(function (value, index) {
      var tmpSubMenuArr = []
      value.subMenu.forEach(function (_value, _index) {
        if (actionList.indexOf(_value.action) > -1 || !_value.action) {
          tmpSubMenuArr.push(_value)
        }
      })
      if (tmpSubMenuArr.length > 0) {
        allArr.push({
          name: value.name,
          icon: value.icon,
          subMenu: tmpSubMenuArr
        })
      }
    })
    return allArr
  }
}
</script>
